/**
*
* AndroidMenu.as
* Niels Overwijn
* 
* Copyright (c) 2010 Niels Overwijn
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package niels.android
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	/**
	 * A simple button menu that looks like the standard android menu
	 * @author Niels Overwijn
	 */
	public class AndroidMenu extends Sprite
	{
		
		private var _items:Array;
		private var _expanded:Boolean;
		
		/**
		 * Constructor
		 * @param item First item in the menu. A menu should always at least contain 1 item
		 */
		public function AndroidMenu(item:AndroidMenuItem) 
		{
			this.addEventListener(Event.ADDED_TO_STAGE, this_addedToStageHandler);
			
			addChild(item);
			item.addEventListener(Event.CLOSE, menu_closeHandler);
			
			_expanded = false;
			_items = new Array();
			_items.push(item);
			
			repositionItems();
			
			this.y = AndroidConfig.HEIGHT;
			this.alpha = 0;
			
		}
		
		//////////////////////
		// PUBLIC FUNCTIONS //
		//////////////////////
		
		/**
		 * Adds a new menu item to the menu
		 * @param	item
		 */
		public function addItem(item:AndroidMenuItem):void
		{
			addChild(item);
			item.addEventListener(Event.CLOSE, menu_closeHandler);
			_items.push(item);
			repositionItems();
		}
		
		/**
		 * Expands the menu
		 */
		public function expand():void
		{
			Tweener.addTween(this, { y:(AndroidConfig.HEIGHT - this.height), time:AndroidConfig.TRANSITION_TIME, transition:AndroidConfig.TRANSITION_TYPE } );
			Tweener.addTween(this, { alpha:1, time:AndroidConfig.TRANSITION_TIME, transition:"linear" } );
			_expanded = true;
			
		}
		
		/**
		 * Collapses the menu
		 */
		public function collapse():void
		{
			Tweener.addTween(this, { y:AndroidConfig.HEIGHT, time:AndroidConfig.TRANSITION_TIME, transition:AndroidConfig.TRANSITION_TYPE } );
			Tweener.addTween(this, { alpha:0, time:AndroidConfig.TRANSITION_TIME, transition:"linear" } );
			_expanded = false;
			
		}
		
		///////////////////////
		// PRIVATE FUNCTIONS //
		///////////////////////
		
		/**
		 * Adds the key down listener to the stage, so keydown input is triggered correctly
		 * @param	e
		 */
		private function this_addedToStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, this_addedToStageHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, this_keyDownHandler);
		}
		
		/**
		 * Repositions all menu items based on the given MAX_ITEMS_IN_ROW and WIDTH
		 */
		private function repositionItems():void
		{
			var rowItems:Array = new Array();
			var totalItems:Number = _items.length;
			while (totalItems > 0)
			{
				if (totalItems >= AndroidConfig.MAX_ITEMS_IN_ROW)
				{
					rowItems.push(AndroidConfig.MAX_ITEMS_IN_ROW);
					totalItems -= AndroidConfig.MAX_ITEMS_IN_ROW;
				} else
				{
					rowItems.push(totalItems);
					totalItems = 0;
				}
			}
			var count:Number = 0;
			for (var i:Number = 0; i < rowItems.length; i++)
			{
				for (var j:Number = 0; j < rowItems[i]; j++)
				{
					_items[count].width = AndroidConfig.MENU_WIDTH / rowItems[i];
					_items[count].x = j * _items[count].width;
					_items[count].y = i * _items[count].height;
					count++;
				}
			}
		}
		
		/**
		 * Collapses the menu
		 * @param	e
		 */
		private function menu_closeHandler(e:Event):void 
		{
			collapse();
		}
		
		/**
		 * Handles the key down event. Checks if the menu should be opened or closed
		 * NOTE: Closes on MENU press or on BACK press, it will prevent the default button action
		 * @param	e
		 */
		private function this_keyDownHandler(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.MENU)
			{
				if (!_expanded)
				{
					expand();
					e.preventDefault();
				} else
				{
					collapse();
					e.preventDefault();
				}
			}
			if (e.keyCode == Keyboard.BACK)
			{
				if (_expanded)
				{
					collapse();
					e.preventDefault();
				}
			}
		}
		
	}

}